home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / EDITSDI.PAK / FINDDLG.C < prev    next >
C/C++ Source or Header  |  1997-05-06  |  6KB  |  232 lines

  1. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
  2. // ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
  3. // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  4. // PARTICULAR PURPOSE.
  5. //
  6. // Copyright (C) 1993-1995  Microsoft Corporation.  All Rights Reserved.
  7. //
  8. //  MODULE: finddlg.c
  9. //
  10. //  PURPOSE: To show the use of the find and replace common dialogs.
  11. //
  12. //
  13. //  FUNCTIONS:
  14. //    InitFindReplace - Register the windows message for find/rep notifications.
  15. //    CmdFindReplace - Opens the find or replace modeless dialog box.
  16. //    MsgFindReplace - Handle the messages generated by the find/replace dialog.
  17. //
  18. //  COMMENTS:
  19. //
  20. //
  21. //
  22. //  SPECIAL INSTRUCTIONS: N/A
  23. //
  24.  
  25. #include <windows.h>            // required for all Windows applications
  26. #ifdef WIN16
  27. #include <commdlg.h>
  28. #include "win16ext.h"           // required only for win16 applications
  29. #endif
  30. #include "globals.h"            // prototypes specific to this application
  31. #include "resource.h"
  32.  
  33. #define CCHMAXFIND 80
  34. #define CCHMAXREPL 80
  35.  
  36. static HWND hdlg = NULL;        // handle identifying the find/rep dialog
  37. static WORD wCommCur = 0;       // Current command - find or replace
  38. static char szFind[CCHMAXFIND] = {'\0'}; // The string to find
  39. static char szRepl[CCHMAXREPL] = {'\0'}; // The string to replace
  40.  
  41. // common dialog box structure
  42. static FINDREPLACE fr = {0,0,0,FR_DOWN|FR_MATCHCASE,0};
  43.  
  44. //
  45. //  FUNCTION: InitFindReplace(VOID)
  46. //
  47. //  PURPOSE: Register the windows message for find/replace notifications
  48. //
  49. //  PARAMETERS:
  50. //    NONE.
  51. //
  52. //  RETURN VALUE:
  53. //    TRUE - Initialization succeeded - the message was registered.
  54. //    FALSE - Initialization failed - the message was not registered.
  55. //
  56. //  COMMENTS:
  57. //
  58. //
  59.  
  60. BOOL InitFindReplace(VOID) {
  61.     msdiMain.rgmsd[0].uMessage = RegisterWindowMessage(FINDMSGSTRING);
  62.     return msdiMain.rgmsd[0].uMessage != 0;
  63. }
  64.  
  65. BOOL IsFindReplaceMsg(LPMSG lpmsg) {
  66.     if (hdlg != NULL)
  67.         return IsDialogMessage(hdlg, lpmsg);
  68.     else
  69.         return FALSE;
  70. }
  71.  
  72. BOOL CanFind(VOID) {
  73.     return szFind[0] != '\0';
  74. }
  75.  
  76.  
  77. //
  78. //  FUNCTION: CmdFindReplace(HWND, WORD, WORD, HWND)
  79. //
  80. //  PURPOSE: Opens the find or replace modeless dialog box.
  81. //
  82. //  PARAMETERS:
  83. //    hwnd     - The window handle.
  84. //    wCommand - IDM_FIND || IDM_REPLACE
  85. //    wNotify   - Notification number (unused)
  86. //    hwndCtrl - NULL (Unused)
  87. //
  88. //  RETURN VALUE:
  89. //    Always returns 0 - message handled.
  90. //
  91. //  COMMENTS:
  92. //    If a find or replace dialog already exists, this command just
  93. //      brings it to the top.
  94. //
  95. //
  96.  
  97. #pragma argsused
  98. LRESULT CmdFindReplace(HWND hwnd, WORD wCommand, WORD wNotify, HWND hwndCtrl) {
  99.     if (hdlg != NULL) {
  100.         if (wCommand == wCommCur) {
  101.             BringWindowToTop(hdlg);
  102.             return 0;
  103.         }
  104.         else {
  105.             DestroyWindow(hdlg);
  106.         }
  107.     }
  108.     wCommCur = wCommand;
  109.  
  110.     // Initialize the find/replace structure
  111.  
  112.     fr.lStructSize = sizeof(FINDREPLACE);
  113.     fr.hwndOwner = hwnd;
  114.     fr.lpstrFindWhat = szFind;
  115.     fr.wFindWhatLen = sizeof(szFind);
  116.     fr.lpstrReplaceWith = szRepl;
  117.     fr.wReplaceWithLen = sizeof(szRepl);
  118.  
  119.     // Display the modeless Find/Replace dialog box.
  120.  
  121.     if (wCommand == IDM_FIND) {
  122.         hdlg = FindText(&fr);
  123.     }
  124.     else {
  125.         hdlg = ReplaceText(&fr);
  126.     }
  127.  
  128.     return 0;
  129. }
  130.  
  131. //
  132. //  FUNCTION: MsgFindReplace(HWND, UINT, WPARAM, LPARAM)
  133. //
  134. //  PURPOSE: Handle the messages generated by the find/replace dialog.
  135. //
  136. //  PARAMETERS:
  137. //
  138. //    hwnd      - Window handle  (Unused)
  139. //    uMessage  - Message number (Unused)
  140. //    wparam    - Extra data     (Unused)
  141. //    lparam    - LPFINDREPLACE
  142. //
  143. //  RETURN VALUE:
  144. //    Always returns 0 - Message handled
  145. //
  146. //  COMMENTS:
  147. //    If FR_DIALOGTERM flag is set, set hdlg to NULL so that the CmdFindReplace
  148. //    function can open another one when it is called.
  149. //    Otherwise call the user defined FindReplace function with the information
  150. //    about the find/replace to do the real work.
  151. //
  152.  
  153. #pragma argsused
  154. LRESULT MsgFindReplace(HWND hwnd, UINT uMessage, WPARAM wparam, LPARAM lparam) {
  155.      LPFINDREPLACE lpfr;
  156.      FRT frt;
  157.  
  158.      // Extract a pointer to the FINDREPLACE structure from lParam.
  159.  
  160.     lpfr = (FINDREPLACE FAR*) lparam;
  161.  
  162.     // If the system has set the FR_DIALOGTERM flag, invalidate the
  163.     //  handle identifying the dialog box.
  164.  
  165.     if (lpfr->Flags & FR_DIALOGTERM){
  166.         hdlg = NULL;
  167.         lpfr->Flags &= ~FR_DIALOGTERM;
  168.         return 0;
  169.     }
  170.  
  171.      // Determine if the request is a find, replace, or replace all.
  172.  
  173.     if (lpfr->Flags & FR_FINDNEXT) {
  174.         frt = frtFind;
  175.     }
  176.     else if (lpfr->Flags & FR_REPLACE) {
  177.         frt = frtReplace;
  178.     }
  179.     else if (lpfr->Flags & FR_REPLACEALL) {
  180.         frt = frtRepAll;
  181.     }
  182.     else {
  183.         return 0;
  184.      }
  185.  
  186.     // If the dialog box is still valid, call the application-defined
  187.     //   search or replace routine
  188.  
  189.     FindReplace(
  190.         szFind, szRepl,
  191.         frt,
  192.         MAKEBOOL(lpfr->Flags & FR_DOWN),
  193.         MAKEBOOL(lpfr->Flags & FR_MATCHCASE),
  194.         MAKEBOOL(lpfr->Flags & FR_WHOLEWORD)
  195.     );
  196.  
  197.      return 0;
  198. }
  199.  
  200. //
  201. //  FUNCTION: CmdFindNext(HWND, WORD, WORD, HWND)
  202. //
  203. //  PURPOSE: Opens the find or replace modeless dialog box.
  204. //
  205. //  PARAMETERS:
  206. //    hwnd     - The window handle.
  207. //    wCommand - IDM_FINDPREV || IDM_FINDNEXT
  208. //    wNotify   - Notification number (unused)
  209. //    hwndCtrl - NULL (Unused)
  210. //
  211. //  RETURN VALUE:
  212. //    Always returns 0 - message handled.
  213. //
  214. //  COMMENTS:
  215. //    If a find or replace dialog already exists, this command just
  216. //      brings it to the top.
  217. //
  218. //
  219.  
  220. #pragma argsused
  221. LRESULT CmdFindNext(HWND hwnd, WORD wCommand, WORD wNotify, HWND hwndCtrl) {
  222.     FindReplace(
  223.         szFind, szRepl,
  224.         frtFind,
  225.         wCommand == IDM_FINDNEXT,
  226.         MAKEBOOL(fr.Flags & FR_MATCHCASE),
  227.         MAKEBOOL(fr.Flags & FR_WHOLEWORD)
  228.     );
  229.  
  230.     return 0;
  231. }
  232.